package com.yzs.base.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import com.yzs.base.R

/**
 * author : yzs
 * time   : 2021/07/06
 * desc   : 渐变蒙版
 */
class MaskView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    init {

        // region 获取值
        val a = context.obtainStyledAttributes(attrs, R.styleable.MaskView, defStyleAttr, 0)
        val startColor = a.getColor(R.styleable.MaskView_mask_start_color, Color.TRANSPARENT)
        val endColor = a.getColor(R.styleable.MaskView_mask_end_color, Color.TRANSPARENT)
        a.recycle()
        val colors = intArrayOf(startColor, endColor)
        this.background = GradientDrawable().apply {
            shape = GradientDrawable.RECTANGLE
            this.colors = colors
            gradientType = GradientDrawable.LINEAR_GRADIENT//设置线性渐变
            setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        }
    }
}